package com.qf.sy2103.springboot.Controller.User;


import com.qf.sy2103.springboot.Service.NumberService;
import com.qf.sy2103.springboot.Util.Constant;
import com.qf.sy2103.springboot.vo.Tablevo;
import com.qf.sy2103.springboot.vo.Uservo;
import com.qf.sy2103.springboot.vo.pagevo;
import com.qf.sy2103.springboot.entity.Number;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Objects;

@Slf4j
@Controller
@RequestMapping("/number")
public class NumberController {

    @Autowired
   private NumberService numberService;

    @ResponseBody
    @RequestMapping("/getAll")
    public Tablevo<Number> getAll(pagevo pagevo, Number number, String states){
        log.debug("入参为 {}",pagevo,number,states);
        if (number.getState()==1&&(Objects.isNull(states))){
            Tablevo<Number> tablevo = numberService.getALL(pagevo);
            log.debug("返回的数据为 {}",tablevo);
            return tablevo;
        }

        if (Objects.nonNull(states)&& states!=""){
            number.setState(Integer.parseInt(states));
        }

       Tablevo<Number> tablevo = numberService.getAll(pagevo,number);
       log.debug("返回的数据为 {}",tablevo);
       return  tablevo;
    }

    @ResponseBody
    @RequestMapping("/add")
    public Uservo addAll(Number number){
        log.debug("入参为 {}",number);
        Uservo uservo = new Uservo();
        Boolean b =numberService.addUse(number);
        if (b){
            //增加成功
            uservo.setStatus(Constant.CLIENT_ADD_SUCCESS);
            return uservo;
        }
        //增加失败
        uservo.setMessage(Constant.CLIENT_ADD_FAL);
        return uservo;
    }

    @ResponseBody
    @RequestMapping("/delete")
    public Uservo deleteAll(@RequestParam(value = "ids",required = false) List<Integer> ids){
        log.debug("入参为 {}",ids);
        Uservo uservo = new Uservo();
        boolean b = numberService.deleteUse(ids);
        if (b){
            //删除成功
            uservo.setStatus(Constant.USER_DELETE_SUCCESS);
            return uservo;
        }
        //删除失败
        uservo.setMessage(Constant.USER_VIEWS_FULE);
        return uservo;
    }

    @ResponseBody
    @RequestMapping("/update")
    public Uservo updateAll(Number number){
        log.debug("入参为 {}",number);
        Uservo uservo = new Uservo();
        Boolean b = numberService.updateUse(number);
        if (b){
            //修改成功
            uservo.setStatus(Constant.CLIENT_UPDATE_SUCCESS);
            return uservo;
        }
        //修改失败
        uservo.setMessage(Constant.USER_UPDATE_FALES);
        return uservo;
    }
}
